//
//  CLDocWeiDianCell.m
//  chanlin
//
//  Created by shuhui on 15-6-20.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLDocWeiDianCell.h"
#import "CLWebVeiwController.h"
#import "CLPushAnimatedRight.h"
#import "CLDocumentModel.h"
#import "UIImageView+WebCache.h"
#import "UIView+Utils.h"
#import "CLDocViewController.h"

@interface CLDocWeiDianCell()
@property (nonatomic, strong) CLDocumentModel *document;

@property (nonatomic, strong)UILabel *introTitle;
@property (nonatomic, strong)UILabel *introLabel;
@property (nonatomic, strong)UIButton *purchaseBtn;
@property (nonatomic, strong)UIImageView *purchaseImg;

@end
@implementation CLDocWeiDianCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _document = [CLDocumentModel new];
        [self creatView];
        
    }
    return self;
}


- (void)creatView {
    
    _introImage = [[UIImageView alloc]init];
    _introImage.backgroundColor = [UIColor clearColor];
    _introImage.contentMode=UIViewContentModeScaleAspectFit;
    _introImage.clipsToBounds=YES;
    
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goImg)];
    [_introImage addGestureRecognizer:tap1];
    
    _introImage.userInteractionEnabled=YES;
    
    [self.contentView addSubview:_introImage];
    
    _introTitle = [[UILabel alloc]init];
    _introTitle.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_introTitle];
    
    _introLabel = [[UILabel alloc]init];
    _introLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_introLabel];
    
    
    _purchaseImg = [[UIImageView alloc]init];
    _purchaseImg.userInteractionEnabled = YES;
    _purchaseImg.contentMode=UIViewContentModeScaleAspectFit;
    _purchaseImg.layer.cornerRadius = 2;
    _purchaseImg.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_purchaseImg];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                        action:@selector(goToWeiDian)];
    [_purchaseImg addGestureRecognizer:tap];

}

- (void)goImg{
    if(self.imgUrl){
        [self.weak didselectINHtmlCell:self];
    }
}

- (void)loadData:(CLDocumentModel *)document {
    _document = document;
    
    if (_isStrNotNull(_document.entity.weiDianUrl) && _isStrNotNull(_document.entity.weiDianPicUrl)){
        float y=0.f;
        if (_isStrNotNull(_document.entity.weiDianPicUrl)) {
            _introImage.frame = CGRectMake(12.5, 20, 295, 160);//[self.document.entity.weiDianPicH intValue]
            [_introImage sd_setImageWithURL:[NSURL URLWithString:_document.entity.weiDianPicUrl]
                           placeholderImage:[UIImage imageNamed:@"index_thumbnail.png"]];
            _introImage.height=document.entity.wdpicHeight;
            self.imgUrl=_document.entity.weiDianPicUrl;
            y=_introImage.bottom;
        }
        
        if (_isStrNotNull(_document.entity.weiDianIntroTitle)) {
            _introTitle.frame = CGRectMake(0, y+20, GlobleWidth, 16);
            _introTitle.text = _document.entity.weiDianIntroTitle;
            _introTitle.font = [UIFont systemFontOfSize:16.f];
            _introTitle.textColor = RGB(0x54, 0x54, 0x54);
            _introTitle.textAlignment = NSTextAlignmentCenter;
            y=_introTitle.bottom;
        }
        
        if (_isStrNotNull(_document.entity.weiDianIntro)) {
            int num = [self getSubStringShowNumsInStringBy:_document.entity.weiDianIntro andSubstring:@"\n"];
            _introLabel.numberOfLines = num+1;
            _introLabel.text = _document.entity.weiDianIntro;
            _introLabel.font = [UIFont systemFontOfSize:13.f];
            _introLabel.textColor = RGB(0x54, 0x54, 0x54);
            _introLabel.textAlignment = NSTextAlignmentCenter;
            
            UIFont *fnt = [UIFont systemFontOfSize:13.f];
            _introLabel.font = fnt;
            CGSize size = [_introLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
            _introLabel.frame = CGRectMake(0, y+20, GlobleWidth, size.height);
            y=_introLabel.bottom;
        }
        
        _purchaseImg.frame = CGRectMake((GlobleWidth-92)/2, y+28, 92, 32);
        if (_isStrNotNull(_document.entity.weiDianBtnImgUrl)) {
            [_purchaseImg sd_setImageWithURL:[NSURL URLWithString:_document.entity.weiDianBtnImgUrl]
                            placeholderImage:[UIImage imageNamed:@"index_thumbnail.png"]];
            
        }else if(_isStrNULL(_document.entity.weiDianBtnImgUrl)){
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 92, 32)];
            label.backgroundColor = [UIColor redColor];
            label.layer.cornerRadius = 2;
            label.text = @"现在请购";
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont systemFontOfSize:14.f];
            [_purchaseImg addSubview:label];
        }
    }
    
}

-(int)getSubStringShowNumsInStringBy:(NSString*)string andSubstring:(NSString*)Substring
{
    int count =0;
    NSRange range = [string rangeOfString:Substring];
    if(range.length>0)
    {
        count++;
        while (range.length>0) {
            int startIndex =range.location+range.length;
            int endIndex = string.length -startIndex;
            string= [string substringWithRange:NSMakeRange(startIndex, endIndex)];
            range = [string rangeOfString:Substring];
            if(range.length>0) 
            { 
                count++; 
            } 
        } 
    } 
    return count; 
}

- (void)goToWeiDian {
    
    CLWebVeiwController *doc=[CLWebVeiwController new];
    doc.isFile=NO;
    doc.itemId = _document.entity.weiDianUrl;
    [[CLPushAnimatedRight sharedInstance] pushController:doc];
}


@end
